﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace GandA.Web.UI.Controls
{
    [ToolboxData("<{0}:Tab runat=server></{0}:Tab>"),
    ParseChildren(typeof(TabItem), DefaultProperty = "TabItems", ChildrenAsProperties = true),
    PersistChildren(false)]
    public class Tab : WebControl
    {
        public Tab()
        {
            ViewState["TabItems"] = new List<TabItem>();
            ViewState["Width"] = Unit.Percentage(100);
            ViewState["Height"] = Unit.Pixel(370);
        }

        #region Propiedades

        [Bindable(true)]
        [Category("Default")]
        [DefaultValue("")]
        [Localizable(true)]
        public List<TabItem> TabItems
        {
            get { return (List<TabItem>)ViewState["TabItems"]; }
            set { ViewState["TabItems"] = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Div; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue(TabStyle.Default)]
        [Localizable(true)]
        public TabStyle StyleTab
        { 
            get
            {
                return ViewState[this.ID + "StyleTab"] == null ? (TabStyle)1 : (TabStyle)ViewState[this.ID + "StyleTab"];
            }
            set { ViewState[this.ID + "StyleTab"] = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Unit Width
        {
            get { return (Unit)ViewState["Width"]; }
            set { ViewState["Width"] = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public override Unit Height
        {
            get { return (Unit)ViewState["Height"]; }
            set { ViewState["Height"] = value; }
        }

        public enum TabStyle
        {
            Default = 1,
            Style2 = 2,
            Style3 = 3,
            Style4 = 4,
        }
        #endregion

        protected override void OnPreRender(EventArgs e)
        {
            bool linkIncluded = false;
            foreach (Control c in Page.Header.Controls)
            {
                if (c.ID == "TabStyle")
                {
                    linkIncluded = true;
                }
            }
            if (!linkIncluded)
            {
                HtmlGenericControl csslink = new HtmlGenericControl("link");
                csslink.ID = "TabStyle";
                switch (StyleTab)
                {
                    case TabStyle.Style2:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.TabStyle2.css"));
                        break;
                    case TabStyle.Style3:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.TabStyle3.css"));
                        break;
                    case TabStyle.Style4:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.TabStyle4.css"));
                        break;
                    default:
                        csslink.Attributes.Add("href", Page.ClientScript.GetWebResourceUrl(this.GetType(), "GandA.Web.UI.Controls.Tab.css"));
                        break;
                }
                csslink.Attributes.Add("type", "text/css");
                csslink.Attributes.Add("rel", "stylesheet");
                Page.Header.Controls.Add(csslink);
            }
            base.OnPreRender(e);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write("<section class='tabs' style='width:" + this.Width.ToString() + "'>");
            int tab = 1;
            foreach (TabItem tabItem in TabItems)
            {
                writer.Write("<input id='tab-" + tab.ToString() + "' type='radio' name='radio-set' class='tab-selector-" + tab.ToString() + "'" + (tab == 1 ? " checked='checked'" : "") + " />");
                writer.Write("<label for='tab-" + tab.ToString() + "' class='tab-label-" + tab.ToString() + "'>" + tabItem.Caption + "</label>");
                tab++;
            }

            writer.Write("<div class='clear-shadow'></div>");
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "content");
            writer.AddStyleAttribute(HtmlTextWriterStyle.Height, Height.ToString());
            base.Render(writer);
            writer.Write("</section>");
        }

        protected override void CreateChildControls()
        {
            int index = 0;
            foreach (TabItem e in TabItems)
            {
                e.Index = index;
                this.Controls.Add(e);
                index++;
            }

            base.CreateChildControls();
        } 
    }

    [ToolboxItem(false)]
    [ToolboxData("<{0}:TabItem runat=server></{0}:TabItem>")]
    [ParseChildren(false), PersistChildren(true)]
    public class TabItem : WebControl
    {
        public TabItem()
        {
            ViewState["caption"] = "Tab";
            ViewState["index"] = 0;
        }

        #region Propiedades
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Caption
        {
            get { return ViewState["caption"].ToString(); }
            set { ViewState["caption"] = value; }
        }

        protected override HtmlTextWriterTag TagKey
        {
            get { return HtmlTextWriterTag.Div; }
        }

        [Bindable(true)]
        [Category("Default")]
        [Localizable(true)]
        public int Index
        {
            get { return (int)ViewState["index"]; }
            set { ViewState["index"] = value; }
        }
        #endregion

        protected override void Render(HtmlTextWriter writer)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Class, "content-" + (this.Index + 1).ToString());
            base.Render(writer);
        }
    }
}
